package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;
import 试题和试卷.paper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;


public class studentRank extends JFrame {

    public static void main(String[] args) {
        //studentRank rank = new studentRank(currentStudent);
        //rankPaperItem item = new rankPaperItem(1,new paper());
        //rank.add(item);
    }


        String[] papername;
        int[] papertime;
        String tableName="";
        int paperNumber=0;
        JPanel mainPanel;
        int currentPage,page,size;
        JLabel num,paperLabel,pageNumberLabel;
        JButton nextButton,preButton,overallButton,backButton;
        ArrayList<rankPaperItem> rankItems = new ArrayList<>();
        ArrayList<paper> papers;
        Student currentstu;

        Font font = new Font("sanserif",Font.BOLD,10);
        Font pageNumberFont = new Font("sanserif",Font.BOLD,12);

        static studentRank rank;
        public studentRank(Student currentStudent){
            currentstu=currentStudent;
            myDatebase queryDateBase=new myDatebase("information_schema");
            myDatebase paperDateBase=new myDatebase("papercollection");
            try {
                String sql="select TABLE_NAME from information_schema.TABLES where information_schema.TABLES.TABLE_NAME='"+currentStudent.getMyClass()+"' ";
                queryDateBase.statement.execute(sql);
                queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql);
                while (queryDateBase.resultSet.next()){
                    tableName=queryDateBase.resultSet.getString(1);
                }/*上面是寻找到currentsutdent所在的班级*/

                String sql1="select TABLE_ROWS from TABLES where TABLE_NAME='"+tableName+"'";
                queryDateBase.statement.execute(sql1);
                queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql1);
                while(queryDateBase.resultSet.next()){
                    paperNumber=queryDateBase.resultSet.getInt(1);
                }/*上面是从TABLE表中确定试卷数，以上是为了通过currentStudent的班级信息连接上数据库自带的存储信息的TABLES表，从而找到
        对应班级的试卷数据库以及对应的试卷库信息*/

                String sql2=String.format("select * from papercollection.%s",tableName);
                papername=new String[paperNumber+1];
                papertime=new int[paperNumber+1];
                paperDateBase.statement.execute(sql2);
                paperDateBase.resultSet=paperDateBase.statement.executeQuery(sql2);
                int inde=0;
                while(paperDateBase.resultSet.next()){
                    papertime[inde]=paperDateBase.resultSet.getInt(2);
                    papername[inde++]=paperDateBase.resultSet.getString(1);

                /*这里是找到对应的班级数据库，从中找到paperTitle输出到String[]里，也就是papername，
                这样就可以再根据paper类中的方法把这些paper的数据加载到一个arraylist：papers中*/
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally{
                queryDateBase.close();
                paperDateBase.close();
            }

            papers=new ArrayList<>(paperNumber);
            for(int i=0;i<paperNumber;i++){
                papers.add(new paper());
            }


            for(int i=0;i<paperNumber;i++){
                papers.get(i).getQuestions(tableName,papername[i]);
                papers.get(i).setName(papername[i]);
                papers.get(i).setTime(papertime[i]);
            }/*将试卷list中每个试卷分别找到他们的questions,为下面打印出每个试卷的基本信息做准备，
        没使用迭代器是因为papername要使用下标*/



            mainPanel = new JPanel();
            JLabel infoLabel = new JLabel("试卷数目:"+paperNumber);
            JLabel numLabel = new JLabel("序号");
            JLabel paperInfoLabel = new JLabel("试卷信息");
            JLabel paperDetailLabel = new JLabel("详情");
            JLabel paperDoLabel = new JLabel("做题");

            preButton = new JButton("上一页");
            preButton.addActionListener(new preListener());
            preButton.setBounds(750,450,70,30);
            preButton.setFont(font);
            preButton.setBackground(Color.lightGray);

            overallButton = new JButton("历次成绩");
            overallButton.addActionListener(new overallListenerWithStu());
            overallButton.setBounds(810,50,90,40);
            //overallButton.setFont(font);
            overallButton.setBackground(Color.lightGray);


            setTitleType(numLabel);
            setTitleType(paperInfoLabel);
            setTitleType(paperDetailLabel);
            setTitleType(paperDoLabel);

            backButton = new JButton("返回");

            mainPanel.setLayout(null);

            backButton.setBackground(Color.lightGray);
            //backButton.addActionListener(new backListener());

            infoLabel.setBounds(80,30,100,40);
            numLabel.setBounds(80,120,60,50);
            paperInfoLabel.setBounds(140,120,600,50);
            paperDetailLabel.setBounds(740,120,80,50);
            paperDoLabel.setBounds(820,120,80,50);
            backButton.setBounds(820,500,80,30);

            mainPanel.add(paperDetailLabel);
            mainPanel.add(paperDoLabel);
            mainPanel.add(numLabel);
            mainPanel.add(infoLabel);
            mainPanel.add(paperInfoLabel);
            mainPanel.add(backButton);
            mainPanel.add(overallButton);


            size =paperNumber;

            page=(size-1)/5+1;
            currentPage=1;

            pageNumberLabel=new JLabel(currentPage+"/"+page);
            mainPanel.add(pageNumberLabel);
            pageNumberLabel.setFont(pageNumberFont);
            pageNumberLabel.setBounds(430,430,80,30);

            for (int i=1;i<=size;i++){
                rankItems.add(new rankPaperItem(i,papers.get(i-1),currentstu));
            }
            if (page==1){
                for(int i=1;i<=size;i++){
                    add(rankItems.get(i-1));
                }
            }else if(page>1){
                for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                    add(rankItems.get(i));
                }
                nextButton = new JButton("下一页");
                nextButton.addActionListener(new studentRank.nextListener());
                nextButton.setBounds(830,450,70,30);
                nextButton.setFont(font);
                nextButton.setBackground(Color.lightGray);
                mainPanel.add(nextButton);
            }



            //backButton.addActionListener(new backListener());
            infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
            infoLabel.setVerticalAlignment(SwingConstants.CENTER);
            this.setSize(980,620);
            mainPanel.setSize(this.getSize());
            mainPanel.setBackground(Color.white);
            this.setBackground(Color.white);
            this.setContentPane(mainPanel);
            this.setLocationRelativeTo(null);//居中
            this.setTitle("学生在线考试平台");
            this.setVisible(true);
            //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public studentRank(){
        myDatebase queryDateBase=new myDatebase("information_schema");
        myDatebase paperDateBase=new myDatebase("papercollection");
        try {
            String sql="select TABLE_NAME from information_schema.TABLES where information_schema.TABLES.TABLE_NAME='"+enterWindow.currentStudent.getMyClass()+"' ";
            queryDateBase.statement.execute(sql);
            queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql);
            while (queryDateBase.resultSet.next()){
                tableName=queryDateBase.resultSet.getString(1);
            }/*上面是寻找到currentsutdent所在的班级*/

            String sql1="select TABLE_ROWS from TABLES where TABLE_NAME='"+tableName+"'";
            queryDateBase.statement.execute(sql1);
            queryDateBase.resultSet=queryDateBase.statement.executeQuery(sql1);
            while(queryDateBase.resultSet.next()){
                paperNumber=queryDateBase.resultSet.getInt(1);
            }/*上面是从TABLE表中确定试卷数，以上是为了通过currentStudent的班级信息连接上数据库自带的存储信息的TABLES表，从而找到
        对应班级的试卷数据库以及对应的试卷库信息*/

            String sql2=String.format("select * from papercollection.%s",tableName);
            papername=new String[paperNumber+1];
            papertime=new int[paperNumber+1];
            paperDateBase.statement.execute(sql2);
            paperDateBase.resultSet=paperDateBase.statement.executeQuery(sql2);
            int inde=0;
            while(paperDateBase.resultSet.next()){
                papertime[inde]=paperDateBase.resultSet.getInt(2);
                papername[inde++]=paperDateBase.resultSet.getString(1);

                /*这里是找到对应的班级数据库，从中找到paperTitle输出到String[]里，也就是papername，
                这样就可以再根据paper类中的方法把这些paper的数据加载到一个arraylist：papers中*/
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            queryDateBase.close();
            paperDateBase.close();
        }

        papers=new ArrayList<>(paperNumber);
        for(int i=0;i<paperNumber;i++){
            papers.add(new paper());
        }


        for(int i=0;i<paperNumber;i++){
            papers.get(i).getQuestions(tableName,papername[i]);
            papers.get(i).setName(papername[i]);
            papers.get(i).setTime(papertime[i]);
        }/*将试卷list中每个试卷分别找到他们的questions,为下面打印出每个试卷的基本信息做准备，
        没使用迭代器是因为papername要使用下标*/



        mainPanel = new JPanel();
        JLabel infoLabel = new JLabel("试卷数目:"+paperNumber);
        JLabel numLabel = new JLabel("序号");
        JLabel paperInfoLabel = new JLabel("试卷信息");
        JLabel paperDetailLabel = new JLabel("详情");
        JLabel paperDoLabel = new JLabel("做题");

        preButton = new JButton("上一页");
        preButton.addActionListener(new preListener());
        preButton.setBounds(750,450,70,30);
        preButton.setFont(font);
        preButton.setBackground(Color.lightGray);

        overallButton = new JButton("历次成绩");
        overallButton.addActionListener(new overallListener());
        overallButton.setBounds(810,50,90,40);
        //overallButton.setFont(font);
        overallButton.setBackground(Color.lightGray);


        setTitleType(numLabel);
        setTitleType(paperInfoLabel);
        setTitleType(paperDetailLabel);
        setTitleType(paperDoLabel);

        backButton = new JButton("返回");

        mainPanel.setLayout(null);

        backButton.setBackground(Color.lightGray);
        backButton.addActionListener(new backListener());

        infoLabel.setBounds(80,30,100,40);
        numLabel.setBounds(80,120,60,50);
        paperInfoLabel.setBounds(140,120,600,50);
        paperDetailLabel.setBounds(740,120,80,50);
        paperDoLabel.setBounds(820,120,80,50);
        backButton.setBounds(820,500,80,30);

        mainPanel.add(paperDetailLabel);
        mainPanel.add(paperDoLabel);
        mainPanel.add(numLabel);
        mainPanel.add(infoLabel);
        mainPanel.add(paperInfoLabel);
        mainPanel.add(backButton);
        mainPanel.add(overallButton);


        size =paperNumber;

        page=(size-1)/5+1;
        currentPage=1;

        pageNumberLabel=new JLabel(currentPage+"/"+page);
        mainPanel.add(pageNumberLabel);
        pageNumberLabel.setFont(pageNumberFont);
        pageNumberLabel.setBounds(430,430,80,30);

        for (int i=1;i<=size;i++){
            rankItems.add(new rankPaperItem(i,papers.get(i-1)));
        }
        if (page==1){
            for(int i=1;i<=size;i++){
                add(rankItems.get(i-1));
            }
        }else if(page>1){
            for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                add(rankItems.get(i));
            }
            nextButton = new JButton("下一页");
            nextButton.addActionListener(new studentRank.nextListener());
            nextButton.setBounds(830,450,70,30);
            nextButton.setFont(font);
            nextButton.setBackground(Color.lightGray);
            mainPanel.add(nextButton);
        }


        infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
        infoLabel.setVerticalAlignment(SwingConstants.CENTER);
        this.setSize(980,620);
        mainPanel.setSize(this.getSize());
        mainPanel.setBackground(Color.white);
        this.setBackground(Color.white);
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setTitle("学生在线考试平台");
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

        public void setTitleType(JLabel label){
            Font bigFont = new Font("sanserif",Font.PLAIN,16);
            label.setBackground(Color.lightGray);
            label.setOpaque(true);
            label.setFont(bigFont);
            label.setBorder(BorderFactory.createLineBorder(Color.darkGray));
            label.setHorizontalAlignment(SwingConstants.CENTER);
        }
        public void setTextType(JLabel label){
            Font smallFont = new Font("sanserif",Font.PLAIN,12);
            label.setBackground(Color.white);
            label.setOpaque(true);
            label.setFont(smallFont);
            label.setBorder(BorderFactory.createLineBorder(Color.lightGray));
            label.setHorizontalAlignment(SwingConstants.CENTER);
        }
        public void setButtonType(JButton button){
            Font smallFont = new Font("sanserif",Font.PLAIN,14);
            button.setBackground(Color.GRAY);
            button.setFont(smallFont);
            button.setBorder(BorderFactory.createLineBorder(Color.darkGray));
            button.setHorizontalAlignment(SwingConstants.CENTER);
        }
        public class backListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {//关闭注册界面

                studentRank.this.dispose();
                new studentWindow("a");

            }
        }
        public class nextListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i= (currentPage-1)*5;i<=currentPage*5-1;i++){
                    remove(rankItems.get(i));
                }
                currentPage++;
                pageNumberLabel.setText(currentPage+"/"+page);
                if(currentPage!=1){
                    mainPanel.add(preButton);}
                if(currentPage==page){
                    mainPanel.remove(nextButton);
                    for(int i= (currentPage-1)*5;i<size;i++){
                        add(rankItems.get(i));
                    }
                }else{
                    for(int i= (currentPage-1)*5;i<=(currentPage*5-1);i++){
                        add(rankItems.get(i));
                    }
                }

                mainPanel.updateUI();

            }
        }
    public class overallListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            overallRankChart demo = new overallRankChart();
            demo.display(enterWindow.currentStudent);

        }
    }

    public class overallListenerWithStu implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            overallRankChart demo = new overallRankChart();
            demo.display(currentstu);

        }
    }
        public class preListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<size;i++){
                    remove(rankItems.get(i));
                }
                if(currentPage==2){
                    mainPanel.remove(preButton);
                }
                mainPanel.updateUI();
                currentPage--;
                if(currentPage==1){
                    mainPanel.remove(preButton);
                }
                pageNumberLabel.setText(currentPage+"/"+page);
                if(currentPage!=page){
                    mainPanel.add(nextButton);
                }
                for(int i= (currentPage-1)*5;i<=currentPage*5;i++){
                    add(rankItems.get(i));
                }

                if(currentPage<1){
                    currentPage=1;
                }
                mainPanel.updateUI();
            }
        }

        public void add(rankPaperItem item){
            mainPanel.add(item.num);
            mainPanel.add(item.paperLabel);
            mainPanel.add(item.scoreButton);
            mainPanel.add(item.classRankButton);
        }
        public void remove(rankPaperItem item){
            mainPanel.remove(item.num);
            mainPanel.remove(item.paperLabel);
            mainPanel.remove(item.scoreButton);
            mainPanel.remove(item.classRankButton);
        }


    }



